package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.mapper.DiscretionMapper;
import com.it.pojo.*;
import com.it.pojo.dto.DiscretionDto;
import com.it.pojo.dto.InStoreDto;
import com.it.service.DiscretionService;
import com.it.util.CodeUtils;
import com.it.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DiscretionServiceImpl implements DiscretionService {
    @Autowired
    private DiscretionMapper discretionMapper;

    @Override
    public int addDiscretion(OutStore outstore) {
        Discretion discretion=new Discretion();
        BeanUtils.copyProperties(outstore,discretion);
        discretion.setDiscretionId(getPrimaryKey());
        discretion.setDiscretionCode(CodeUtils.getCodeByCountDT(Integer.parseInt(getPrimaryKey())));//自行利用
        discretion.setDiscretionUnit(outstore.getOutUnit());
        discretion.setDiscretionNum(outstore.getOutNum().intValue());
//        discretion.setDiscretionType("1");
        if(outstore.getOutType().equals("1")){
            discretion.setDiscretionType("焚烧");
        } else if (outstore.getOutType().equals("2")) {
            discretion.setDiscretionType("水载");
        }else if (outstore.getOutType().equals("3")){
            discretion.setDiscretionType("填埋");
        }else{
            discretion.setDiscretionType(null);
        }

        discretion.setDiscretionDel(0);

        int i = discretionMapper.insertSelective(discretion);
        return i;
    }

    @Override
    public PageInfo<Discretion> findPage(DiscretionDto dto) {
        if (dto.getPageNo() == null) {
            dto.setPageNo(1);
        }
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        DiscretionExample example = new DiscretionExample();
        DiscretionExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andDiscretionTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andDiscretionTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andDiscretionTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andDiscretionTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("discretion_time asc");
        List<Discretion> list = discretionMapper.selectByExample(example);
        PageInfo<Discretion> pageInfo = new PageInfo<>(list, 5);
        return pageInfo;
    }

    @Override
    public List<Discretion> findExcel(DiscretionDto dto) {
        DiscretionExample example = new DiscretionExample();
        DiscretionExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andDiscretionTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andDiscretionTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andDiscretionTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andDiscretionTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("discretion_time asc");
        return discretionMapper.selectByExample(example);
    }

    public String getPrimaryKey(){
        DiscretionExample discretionExample=new DiscretionExample();
        List<Discretion> discretions = discretionMapper.selectByExample(discretionExample);
        return discretions.size()+1+"";
    }
}
